package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.ui.DrawableSwitch;
import com.argrace.smart.util.AlinkHttpUtil;
import com.zhouyou.view.seekbar.SignSeekBar;

import java.util.HashMap;

/**
 * Created by argrace_dev_1 on 2018/2/11.
 */

public class DeviceControl_ShuiJinActivity extends BaseActivity {
    private ImageView img;
    private SignSeekBar dianliang, xinhao;
    private DrawableSwitch fangqiao, jinshui;
    MineZigBeeDeviceListEntity bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_shuijin);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        setView();
        setTitle(true, false, bean.getName());
        showRight(true, R.mipmap.icon_modify);
        //进入设备修改界面
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeviceControl_ShuiJinActivity.this, DeviceModifyActivity.class);
                intent.putExtra("uuid", bean.getUuid());
                intent.putExtra("model", bean.getModel());
                startActivity(intent);
            }
        });
        getDeviceDetailInfo();
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        String imgurl = ConstantConfigure.IMGURL_SHUIJINTANCEQI_005D1E3E;
        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }
        dianliang = (SignSeekBar) findViewById(R.id.activity_devicecontrol_shuijin_dianliang);
        xinhao = (SignSeekBar) findViewById(R.id.activity_devicecontrol_shuijin_xinhaoqiangdu);
        fangqiao = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_shuijin_fangqiao);
        jinshui = (DrawableSwitch) findViewById(R.id.activity_devicecontrol_shuijin_jinshui);
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastActBtm("不可点击");
            }
        });
    }

    //查询设备最新状态
    private void getDeviceDetailInfo() {
        String method = "mtop.openalink.app.core.device.get.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", bean.getUuid());
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse == null || transitoryResponse.data == null) {
                    return;
                }
                Log.i("info", "子设备最新状态成功" + transitoryResponse.data);
                JSONObject obj = JSON.parseObject(transitoryResponse.data.toString());
                fillData(obj, obj.getString("uuid"));
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "子设备最新状态失败" + aError.getMsg() + aError.getCode());
            }
        });
    }

    @Override
    protected void dorecieve(JSONObject obj) {
        super.dorecieve(obj);
        if (obj == null) {
            return;
        }
        if (obj == null || obj.getJSONObject("params") == null || obj.getJSONObject("params").getJSONObject("data") == null || obj.getJSONObject("params").getString("uuid") == null) {
            return;
        }
        fillData(obj.getJSONObject("params").getJSONObject("data"), obj.getJSONObject("params").getString("uuid"));

    }

    private void fillData(JSONObject obj, String uuid) {
        if (uuid == null || !uuid.equals(bean.getUuid())) {
            return;
        }
        Key_ValueEntity bean_dianliang = JSON.parseObject(obj.getString("BatteryPercentage"), Key_ValueEntity.class);
        Key_ValueEntity bean_xinhao = JSON.parseObject(obj.getString("Rssi"), Key_ValueEntity.class);
        Key_ValueEntity bean_fangqiao = JSON.parseObject(obj.getString("TamperAlarm"), Key_ValueEntity.class);
        Key_ValueEntity bean_jinshui = JSON.parseObject(obj.getString("WaterAlarm"), Key_ValueEntity.class);
        if (bean_dianliang != null) {
            dianliang.setProgress(Long.parseLong(bean_dianliang.getValue()));
        }
        if (bean_xinhao != null) {
            xinhao.setProgress(Long.parseLong(bean_xinhao.getValue()));
        }
        dianliang.getConfigBuilder()
                .sectionTextColor(ContextCompat.getColor(DeviceControl_ShuiJinActivity.this, R.color.colorPrimary))
                .sectionTextSize(15)
                .setUnit("%")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                .build();
        xinhao.getConfigBuilder()
                .sectionTextColor(ContextCompat.getColor(DeviceControl_ShuiJinActivity.this, R.color.colorPrimary))
                .sectionTextSize(15)
                .setUnit("%")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                .build();
        if (bean_fangqiao != null && bean_fangqiao.getValue().equals("0")) {
            fangqiao.setSwitchOn(false);
        } else {
            fangqiao.setSwitchOn(true);
        }
        if (bean_jinshui != null && bean_jinshui.getValue().equals("0")) {
            jinshui.setSwitchOn(false);
        } else {
            jinshui.setSwitchOn(true);
        }
        fangqiao.invalidate();
        jinshui.invalidate();
    }


}
